package com.huahua.api.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.huahua.api.entity.dto.holiday.HolidayCountriesDto;
import com.huahua.api.entity.dto.holiday.HolidayDto;
import com.huahua.api.entity.pojo.ApiHoliday;
import com.huahua.api.entity.pojo.ApiHolidayCountries;
import com.huahua.api.entity.pojo.ApiHolidayType;
import com.huahua.api.model.LocalData;
import com.huahua.api.service.ApiCountriesService;
import com.huahua.api.service.ApiHolidayService;
import com.huahua.api.service.ApiHolidayTypeService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LoveHuahua
 * @date 2021年09月05日 16:49
 * @description believe in yourself
 */
@Component
public class HolidayApi {

    // 根地址
    private static final String api = "http://www.shijian.cc";
    // 获取国家列表
    private static final String getApiCountries = api + "/jieri/";
    @Autowired
    ApiCountriesService apiCountriesService;
    @Autowired
    ApiHolidayService apiHolidayService;
    @Autowired
    ApiHolidayTypeService typeService;

    public List<ApiHoliday> getCountries() throws IOException {
        for (ApiHolidayType apiHolidayType : typeService.list()) {
            LocalData.setApiHolidayType(apiHolidayType.getType(), apiHolidayType.getId());
        }
        for (ApiHolidayCountries apiHolidayCountries : apiCountriesService.list()) {
            LocalData.setApiHolidayCountries(apiHolidayCountries.getName(), apiHolidayCountries);
        }

        Element body = Jsoup.connect(getApiCountries).get().body();
        List<ApiHoliday> apiHolidayList = Lists.newArrayList();
        for (Element aLink : body.select("tbody > tr > td > a")) {
            String apiHref = aLink.attr("href");
            String countriesName = aLink.select("span").text();

            ApiHolidayCountries entity = new ApiHolidayCountries();
            String countriesFinalName = countriesName.replaceAll("节假日", "");
            entity.setName(countriesFinalName);

            //自动添加国家
            Long count = apiCountriesService.lambdaQuery().eq(ApiHolidayCountries::getName, countriesFinalName).count();
            if (count == 0) {
                apiCountriesService.save(entity);
                LocalData.setApiHolidayCountries(entity.getName(), entity);
            }

            HolidayCountriesDto holidayCountriesDto = new HolidayCountriesDto(api + apiHref, countriesFinalName);
            apiHolidayList.addAll(getYearList(holidayCountriesDto));
        }
        return apiHolidayList;
    }

    /**
     * 根据国家获取 节日
     *
     * @param dto 国家的Dto
     * @throws IOException
     */
    private List<ApiHoliday> getYearList(HolidayCountriesDto dto) throws IOException {
        ArrayList<ApiHoliday> holidayDtos = Lists.newArrayList();
        Document document = Jsoup.connect(dto.getHref()).get();
        Elements select1 = document.select("li.dropdown > ul > li ");
        for (Element element : select1) {
            String href = element.select("a").attr("href");
            String year = element.select("a").text();
            holidayDtos.addAll(getHoliday(dto.getName(),api +  href));
            try {
                Thread.sleep(3000 * 10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return holidayDtos;
    }

    private List<ApiHoliday> getHoliday(String name, String url) throws IOException {
        Document document = Jsoup.connect(url).timeout(30000).get();
        ArrayList<ApiHoliday> holidayDtos = Lists.newArrayList();
        Elements select = document.select("div > .row.portfolio-block");
        for (Element element : select) {
            String date = element.select("div.col-md-3.col-sm-3.col-xs-3 > div > div > h3").text();
            String jieriName = element.select("div.col-md-4.col-sm-4.col-xs-4 > div > div > h3 > a ").text();
            String jieriDesc = element.select("div.col-md-4.col-sm-4.col-xs-4 > div > div > p ").text();
            String type = element.select("div.col-md-3.col-sm-3.col-xs-3.portfolio-stat > div > span").text();
            HolidayDto e = new HolidayDto();
            e.setHolidayName(jieriName);
            if (StringUtils.isEmpty(date)){
                continue;
            }
            e.setDate(LocalDate.parse(date));
            e.setHolidayType(type);
            e.setHolidayDesc(jieriDesc);
            //填充国家id
            e.setCountryId(LocalData.getApiHolidayCountries(name).getId());
            //自动添加类型
            if (LocalData.getApiHolidayType(type) == null) {
                ApiHolidayType entity = new ApiHolidayType();
                entity.setType(type);
                typeService.save(entity);
                LocalData.setApiHolidayType(type, entity.getId());
            }
            //自动填充类型
            holidayDtos.add(e.convert());
            System.out.println("================");
        }
        return holidayDtos;
    }

}
